package org.tony.sim.fmt;

public class Fmt32 extends Fmt {
    private static final byte FMT_LENGTH = 32;
    protected static final byte[] OPCODE_BITS = new byte[]{6, 5, 4, 3, 2, 1, 0};
    protected static final byte[] RD_BITS = new byte[]{11, 10, 9, 8, 7};
    protected static final byte[] FUNCT3_BITS = new byte[]{14, 13, 12};
    protected static final byte[] RS1_BITS = new byte[]{19, 18, 17, 16, 15};
    protected static final byte[] RS2_BITS = new byte[]{24, 23, 22, 21, 20};
    protected static final byte[] FUNCT7_BITS = new byte[]{31, 30, 29, 28, 27, 26, 25};

    protected int inst = 0;

    protected Fmt32(Module module, Category category, String name, String description, int inst) {
        super(module, category, name, description, FMT_LENGTH, OPCODE_BITS);
        this.inst = inst;
    }

    public byte getOpcode() {
        return (byte) getBits(inst, opcodeBits);
    }

    public void setOpcode(byte opcode) {
        inst = setBits(inst, opcode, opcodeBits);
    }

    @Override
    public String toString() {
        String ret = super.toString();
        return ret.substring(0, ret.length() - 1) +
                ", \"instruction value\":\"" + transToBinaryString(inst, getFmtLength()) + "\"}";
    }
}
